package br.inf.solutions.apresentacoes.ssp.view.composer.hierarchy;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.zkoss.zk.ui.Component;

import br.com.vexillum.util.HibernateUtils;
import br.com.vexillum.util.ReflectionUtils;
import br.com.vexillum.util.SpringFactory;
import br.com.vexillum.vexhierarchia.view.composer.GenericHierarchyEntityComposer;
import br.inf.solutions.apresentacoes.ssp.control.hierarchy.HierarchyClassificationController;
import br.inf.solutions.apresentacoes.ssp.control.hierarchy.HierarchyStructEntityController;
import br.inf.solutions.apresentacoes.ssp.model.hierarchy.HierarchyClassification;
import br.inf.solutions.apresentacoes.ssp.model.hierarchy.HierarchyStructEntity;
import br.inf.solutions.apresentacoes.ssp.model.hierarchy.HierarchyStructType;

@SuppressWarnings("serial")
@org.springframework.stereotype.Component
@Scope("prototype")
public class HierarchyStructFormComposer extends GenericHierarchyEntityComposer<HierarchyStructEntity, HierarchyStructEntityController>{

	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		loadBinder();
	}
	
	@Override
	public HierarchyStructEntityController getControl() {
		return SpringFactory.getController("hierarchyStructEntityController", HierarchyStructEntityController.class, ReflectionUtils.prepareDataForPersistence(this));
	}

	@Override
	public HierarchyStructEntity getEntityObject() {
		HierarchyStructEntity struct = new HierarchyStructEntity();
		if (struct.getType() == null)
		{
			HierarchyStructType structType = (HierarchyStructType) arg.get("entityType");
			struct.setType(structType);
		}
		return struct;
	}
	
	@Deprecated                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
	public String getLabelClassification()
	{
		return "Tipo de " + entity.getType().getName();
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public List getClassifications()
	{
		HierarchyClassificationController controller = SpringFactory.getController(
				"hierarchyClassificationController", HierarchyClassificationController.class, null);
		controller.getData().put("sql", "from HierarchyClassification where active=true and " +
				"id > 1 and type.id = " +  entity.getType().getId());
		ArrayList<HierarchyClassification> classifications = (ArrayList<HierarchyClassification>) controller.searchByHQL().getList();
		ArrayList<HierarchyClassification> classificationsInicialized = new ArrayList<HierarchyClassification>();
		for(HierarchyClassification classification: classifications)
		{
			classification = (HierarchyClassification) HibernateUtils.materializeProxy(classification);
			classificationsInicialized.add(classification);
		}
	return classificationsInicialized; 
	}		
}